package com.jy.horny.function

import android.app.Activity
import android.util.Log
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import androidx.core.view.size
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.google.android.material.bottomnavigation.BottomNavigationItemView
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.android.material.navigation.NavigationBarView
import com.lib.module_res.R
import com.jy.horny.databinding.ViewNavigationNumBinding

object NavigationHelper {
    fun init(activity : Activity, navView: BottomNavigationView) {
        val viewGroup = navView.getChildAt(0) as ViewGroup
        Log.e("~~~", "init: " + viewGroup.size)
        for (index in 0..< viewGroup.size) {
            val tabView =  viewGroup.getChildAt(index)
            val navigationViewItem = tabView as BottomNavigationItemView
            val viewNavigationNumBinding = ViewNavigationNumBinding.inflate(LayoutInflater.from(activity))
            navigationViewItem.addView(viewNavigationNumBinding.root,0)
            if (index == 2){
                Glide.with(activity)
                    .load("https://storage.googleapis.com/meboo-online-b68e7.appspot.com/icons/body-shape/woman/Stocky.png")
                    .apply(RequestOptions.bitmapTransform(CircleCrop()))
                    .into(viewNavigationNumBinding.ivTab)
                viewNavigationNumBinding.ivTab.setBackgroundResource(if (navigationViewItem.isSelected)R.drawable.tab_me_select_bg else R.drawable.tab_me_unselect_bg)
            }else {
                if (navigationViewItem.isSelected) {
                    viewNavigationNumBinding.ivTab.setImageResource(if (index == 0) R.mipmap.tab_index_select else R.mipmap.tab_chat_select)
                } else {
                    viewNavigationNumBinding.ivTab.setImageResource(if (index == 0) R.mipmap.tab_index_unselect else R.mipmap.tab_chat_unselect)
                }
            }
        }
        navView.setOnItemSelectedListener(object : NavigationBarView.OnItemSelectedListener {
            override fun onNavigationItemSelected(item: MenuItem): Boolean {
                when(item.itemId){
                    com.jy.horny.R.id.navigation_dashboard->{

                        return true
                    }
                    com.jy.horny.R.id.navigation_home->{
                        for (index in 0..< viewGroup.childCount){
                            val tabView =  viewGroup.getChildAt(index)
                            val navigationViewItem = tabView as BottomNavigationItemView
                            val view = navigationViewItem.getChildAt(0)
                            Log.e("~~~", "onNavigationItemSelected: " + navigationViewItem.isSelected )
                            val viewNavigationNumBinding = ViewNavigationNumBinding.bind(view)
                            if (index == 2){
                                Glide.with(activity)
                                    .load("https://storage.googleapis.com/meboo-online-b68e7.appspot.com/icons/body-shape/woman/Stocky.png")
                                    .apply(RequestOptions.bitmapTransform(CircleCrop()))
                                    .into(viewNavigationNumBinding.ivTab)
                                viewNavigationNumBinding.ivTab.setBackgroundResource(if (navigationViewItem.isSelected)R.drawable.tab_me_select_bg else R.drawable.tab_me_unselect_bg)
                            }else {
                                if (navigationViewItem.isSelected) {
                                    viewNavigationNumBinding.ivTab.setImageResource(if (index == 0) R.mipmap.tab_index_select else R.mipmap.tab_chat_select)
                                } else {
                                    viewNavigationNumBinding.ivTab.setImageResource(if (index == 0) R.mipmap.tab_index_unselect else R.mipmap.tab_chat_unselect)
                                }
                            }
                        }
                        return true

                    }
                    com.jy.horny.R.id.navigation_notifications->{
                        return true
                    }

                }

                return false

            }

        })
    }
    private fun View.clickListener(event: () -> Unit) {
        this.setOnTouchListener(object : View.OnTouchListener {
            var x: Int = 0
            var y: Int = 0
            var nowX: Int = 0
            var nowY: Int = 0
            override fun onTouch(p0: View?, p1: MotionEvent?): Boolean {
                when (p1?.action) {
                    MotionEvent.ACTION_DOWN -> {
                        x = p1.x.toInt()
                        y = p1.y.toInt()
                    }

                    MotionEvent.ACTION_MOVE -> {
                        nowX = p1.x.toInt()
                        nowY = p1.y.toInt()
                    }

                    MotionEvent.ACTION_UP -> {
                        if (Math.abs(nowX - x) < 10 || Math.abs(nowY - y) < 10) {
                            //点击事件
                            event.invoke()
                        }
                    }
                }
                event.invoke()
                return false
            }

        })
    }

    private fun clickListener(bottomNavigationView: BottomNavigationView) {
        val viewGroup = bottomNavigationView.getChildAt(0) as ViewGroup
        for (index in 0..< viewGroup.childCount){
            val tabView =  viewGroup.getChildAt(index)
            val navigationViewItem = tabView as BottomNavigationItemView
            val view = navigationViewItem.getChildAt(0)
            val viewNavigationNumBinding = ViewNavigationNumBinding.bind(view)
//            if (this != viewNavigationNumBinding.tabAnimation){
//                viewNavigationNumBinding.tabAnimation.visibility = View.GONE
//                viewNavigationNumBinding.tvTitle.setTextColor(this.context.getColor(R.color.purple_500))
//            }else{
//                viewNavigationNumBinding.tvTitle.setTextColor(this.context.getColor(R.color.teal_200))
//            }
        }
    }

}